package com.shpng.cheapenglish.persistent;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.shpng.cheapenglish.db.UserWord;

public class UserWordPersistent {

	private UserWordPersistent() {

	}

	private static UserWordPersistent userWordPersistent;

	public static UserWordPersistent getInstance() {
		if (userWordPersistent == null)
			userWordPersistent = new UserWordPersistent();
		return userWordPersistent;
	}

	public void saveUserWord(UserWord userWord, PersistenceManager pm) {// managed
																		// by a
																		// different
		// Object Manager
		pm.makePersistent(userWord);
		System.out.println("save userWord " + userWord.getUnitId() + " "
				+ userWord.getWordId());
	}

	public void deleteUserWord(Long userWordId, PersistenceManager pm) {
		pm.deletePersistent(pm.getObjectById(UserWord.class, userWordId));
	}

	@SuppressWarnings("unchecked")
	public List<UserWord> getUserWords(PersistenceManager pm) {
		Query query = pm.newQuery(UserWord.class);
		List<UserWord> userWords = (List<UserWord>) query.execute();
		return userWords;

	}

}
